﻿using xtraSMSCore;
using xtraSMSProviderHandler;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace xtraSMSGui
{
    public partial class frmLogin : Form
    {
        private Core mainCore;

        public frmLogin()
        {
            InitializeComponent();
            mainCore = new Core();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            mainCore.PluginFolder = "Providers";
            foreach (IProvider provider in mainCore.ProviderList)
            {
                cmbProvider.Items.Add(provider.Name);
            }
        }

        private void openSendForm()
        {
            frmSend cpSend = new frmSend(mainCore);
            cpSend.ShowDialog();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            foreach (IProvider provider in mainCore.ProviderList)
            {
                if(cmbProvider.SelectedItem.Equals(provider.Name))
                {
                    mainCore.SetProvider(provider);
                }
            }
            if (mainCore.Login(tbLogin.Text, tbPassword.Text))
            {
                mainCore.SaveCredentials = ckbRemember.Checked;
                mainCore.UseOutlook = ckbOutlook.Checked;                
                System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(openSendForm));
                t.Start();
                this.Close();
            }
            else
            {
                MessageBox.Show("Login failed!!!", "Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

    }
}